package com.batchengine.rssfeed.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.CASE_INSENSITIVE;
import static java.util.regex.Pattern.compile;

/**
 * Created by sunil.kalva@mkhoj.com.
 * User: sunil.kalva
 * Date: Dec 6, 2009
 */
public class FeedMessage {

    private static final Pattern p = compile(".*<img[^>]*src=\"([^\"]*)", CASE_INSENSITIVE);

    String title;
    String description;
    String link;
    String author;
    String guid;
    String category;
    String image;

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getImage() {
        if(null == image) {
            Matcher m = p.matcher(description);
            if(m.find()) {
                image = m.group(1);
                return image;
            }
        }
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getGuid() {
        return guid;
    }

    public void setGuid(String guid) {
        this.guid = guid;
    }

    @Override
    public boolean equals(Object o) {
        if(this == o) return true;
        if(!(o instanceof FeedMessage)) return false;

        FeedMessage that = (FeedMessage) o;

        return !(author != null ? !author.equals(that.author) : that.author != null) && !(guid != null ? !guid.equals(that.guid) : that.guid != null) &&
                !(link != null ? !link.equals(that.link) : that.link != null) && !(title != null ? !title.equals(that.title) : that.title != null);

    }

    @Override
    public int hashCode() {
        int result = title != null ? title.hashCode() : 0;
        result = 31 * result + (link != null ? link.hashCode() : 0);
        result = 31 * result + (author != null ? author.hashCode() : 0);
        result = 31 * result + (guid != null ? guid.hashCode() : 0);
        return result;
    }

    public String toString() {
        return "FeedMessage{" +
                "title='" + title + '\'' +
                ", description='" + description + '\'' +
                ", link='" + link + '\'' +
                ", author='" + author + '\'' +
                ", guid='" + guid + '\'' +
                ", category='" + category + '\'' +
                ", image='" + getImage() + '\'' +
                '}';
    }
}

